<?php
	require_once 'Configuration.php';

	require_once LIB_PATH . '/log4php/Logger.php'; 
	require_once BEAN_PATH . '/Rilevazione.php';
	require_once DAO_PATH . '/RilevazioneDao.php';
	require_once HELPER_PATH . '/HtmlHelper.php';

	$log = Logger::getLogger('main');
	
	$time_start = time();
	
	$log->info("Batch started.");
	
	$time_sub_start = time();
	$ini_set_count = 0;
				
	$url = "http://txodds.com/omoves.php?ot=0";
	
	try{
		$html = HtmlHelper::getHtmlFromURL($url);
	} catch(Exception $e){
		$log->error('ERRORE: '.$e->getMessage());
	}
	
	$i = 0;
	
	foreach($html->find('.data') as $row) {
		$i++;

		$id_match_raw = $row->id;
		$id_match_temp = str_replace('omvt_', '', $id_match_raw);
		$id_match = str_replace('_0', '', $id_match_temp);
	    
		$datetime_match_raw = $row->children(0)->children(0)->innertext;
	    $datetime_match_temp = strstr($datetime_match_raw, 'Date(');
	    $datetime_match_temp = str_replace('Date(', '', $datetime_match_temp);
	    $datetime_match = str_replace(')));', '', $datetime_match_temp);
	    
	    $rilevazione = new Rilevazione();
	    
	    $rilevazione->setId_match($id_match);
	    $rilevazione->setDatetime_match($datetime_match);
	    $rilevazione->setMatch($row->find('.omvtteams', 0)->plaintext);
		$rilevazione->setDatetime_rilev(time());
		$rilevazione->setN_bookmakers_1($row->children(2)->innertext);
		$rilevazione->setMov_down_1($row->children(3)->innertext);
		$rilevazione->setMov_up_1($row->children(4)->innertext);
		$rilevazione->setQuot_in_avg_1($row->children(5)->innertext);
		$rilevazione->setQuot_ou_avg_1($row->children(6)->innertext);
		$rilevazione->setN_bookmakers_X($row->children(7)->innertext);
		$rilevazione->setMov_down_X($row->children(8)->innertext);
		$rilevazione->setMov_up_X($row->children(9)->innertext);
		$rilevazione->setQuot_in_avg_X($row->children(10)->innertext);
		$rilevazione->setQuot_ou_avg_X($row->children(11)->innertext);
		$rilevazione->setN_bookmakers_2($row->children(12)->innertext);
		$rilevazione->setMov_down_2($row->children(13)->innertext);
		$rilevazione->setMov_up_2($row->children(14)->innertext);
		$rilevazione->setQuot_in_avg_2($row->children(15)->innertext);
		$rilevazione->setQuot_ou_avg_2($row->children(16)->innertext);
		$rilevazione->setOci($row->children(17)->plaintext);
		$rilevazione->setVolume_betfair_1(0);
		$rilevazione->setVolume_betfair_X(0);
		$rilevazione->setVolume_betfair_2(0);
		
		//print $rilevazione."<br/>";
		$seconds = ($datetime_match / 1000) + 3600; //+1 hour for timezone offset
		echo date("d-m-Y H:i", $seconds)." ".$rilevazione->getMatch()." ".date("d-m-Y H:i", $rilevazione->getDatetime_rilev()+3600)."<br/>";
		
		$rilevazioneDao = new RilevazioneDao();
		$rilevazioneDao->insert($rilevazione);
		
		$time_end = time();
		if ($time_end - $time_sub_start > 8){
			$ini_set_count++;
			ini_set('max_execution_time', 10);
			$time_sub_start = time();
		} 
	}		
	
	$log->info("Execution time: " . ($time_end - $time_start) . " sec");
	$log->info("Max execution time set: " . $ini_set_count . " times");